#!/bin/bash
#2019/5/28
#zk编写
#第16次修改

SSHVerSion=${1}
OPENSSHVERSION=8.0p1
[[ $SSHVerSion =~ ^[0-9]+.[0-9][a-Z][0-9]$ ]] || SSHVerSion=${OPENSSHVERSION}
#openssh 版本
#openssh version
#从命令行接收参数1ssh版本，默认为8.0p1

SSLVerSion=${2}
OPENSSLVERSION=1.0.2r
[[ $SSLVerSion =~ ^[0-9].[0-9].[0-9][a-Z]$ ]] ||  SSLVerSion=${OPENSSLVERSION}
#openssl版本
#Openssl version
#从命令行接收参数2为ssl版本，默认为1.0.2r

OUTok=/tmp/okfile
#输出的信息会保存到至此文件
#The output information is saved to this file.

OUTerror=/tmp/errorfile

#输出的报错信息会保存到至此文件
#The output error message is saved to this file.

TARTFILE=/tmp/159080113
if [[ ${TARTFILE} =~ ^/+$ ]]
then
        echo "目录不能为根"
        exit
fi
[[ ${TARTFILE} =~ .*/$ ]] && TARTFILE=`echo ${TARTFILE} | sed -n 's/\/$//p'`
#请不要将此变量改成已有目录，脚本结束后会删除此目录！！！！！！
#Do not change this variable to an existing directory and it will be deleted！！！！！！
#建议默认即可

IpS=192.168.40.1:8080
#ping ${IpS} -c 1 &>/dev/null
#http服务器存放升级用到的包,可以更换成其他比如ftp但是需要更改脚本
#Http server stores the packages used for the upgrade，You can switch to another service but you need to change the script.

Server=http
#提供存放包的服务可使用匿名的ftp
Server_DiR=/tools
[[ ${Server_DiR} =~ .*/$ ]] && Server_DiR=`echo ${Server_DiR} | sed -n 's/\/$//p'`
#包存放的路径node:此路径是其他机器通过wget访问ip或者域名后面接的
#比如http://your_ip/tools/tar 此时下面需要用到的包都会从这个位置下载

ScrIpt=$Server_DiR/scripts
[[ ${ScrIpt} =~ .*/$ ]] && ScrIpt=`echo ${ScrIpt} | sed -n 's/\/$//p'`
#脚本存放服务器路径
SoftWare=$Server_DiR/software
[[ ${SoftWare} =~ .*/$ ]] && SoftWare=`echo ${SoftWare} | sed -n 's/\/$//p'`
#软件存放服务器路径
curl ${Server}://${IpS} &>/dev/null
if [ $? -ne 0 ]
then
      echo "${IpS}无法连通，请检查。"
      exit
fi

SSLPREHOME=/usr/local
SSHPREHOME=/usr/local
#openssl和openssh安装路径，请填写绝对路径
[[ ${SSHPREHOME} =~ .*/$ ]] && SSHPREHOME=`echo ${SSHPREHOME} | sed -n 's/\/$//p'`
[[ ${SSLPREHOME} =~ .*/$ ]] && SSLPREHOME=`echo ${SSLPREHOME} | sed -n 's/\/$//p'`
#

ZliBVerSion=1.2.11
ZLIB_TAR=zlib-${ZliBVerSion}.tar.gz
######Wget######
Wget5_RPM=wget-1.11.4-3.el5_8.2.x86_64.rpm
Wget6_RPM=wget-1.12-10.el6.x86_64.rpm
Wget7_RPM=wget-1.14-15.el7.x86_64.rpm
Wget8_RPM=wget-1.19.5-7.el8.x86_64.rpm
######Net-tools######
Net5_RPM=net-tools-1.60-83.el5_10.x86_64.rpm
Net6_RPM=net-tools-1.60-114.el6.x86_64.rpm
Net7_RPM=net-tools-2.0-0.24.20131004git.el7.x86_64.rpm
Net8_RPM=net-tools-2.0-0.51.20160912git.el8.x86_64.rpm
######PAM######
PAM5VerSion=0.99.6.2-12
PAM6VerSion=1.1.1-24
PAM7VerSion=1.1.8-22
PAM8VerSion=1.3.1-4
PAM5_RPM=pam-${PAM5VerSion}.el5.x86_64.rpm
PAM6_RPM=pam-${PAM6VerSion}.el6.x86_64.rpm
PAM7_RPM=pam-${PAM7VerSion}.el7.x86_64.rpm
PAM8_RPM=pam-${PAM8VerSion}.el8.x86_64.rpm
PAM5_DEVEL_RPM=pam-devel-${PAM5VerSion}.el5.x86_64.rpm
PAM6_DEVEL_RPM=pam-devel-${PAM6VerSion}.el6.x86_64.rpm
PAM7_DEVEL_RPM=pam-devel-${PAM7VerSion}.el7.x86_64.rpm
PAM8_DEVEL_RPM=pam-devel-${PAM8VerSion}.el8.x86_64.rpm
######TELNET######
TELNET5VerSion=0.17-41
TELNET6VerSion=0.17-48
TELNET7VerSion=0.17-64
TELNET8VerSion=0.17-73
TELNET5_SERVER_RPM=telnet-server-${TELNET5VerSion}.el5.x86_64.rpm
TELNET6_SERVER_RPM=telnet-server-${TELNET6VerSion}.el6.x86_64.rpm
TELNET7_SERVER_RPM=telnet-server-${TELNET7VerSion}.el7.x86_64.rpm
TELNET8_SERVER_RPM=telnet-server-${TELNET8VerSion}.el8.x86_64.rpm
######XINET######
XINET5VerSion=2.3.14-19
XINET6VerSion=2.3.14-40
XINET5_RPM=xinetd-${XINET5VerSion}.el5.x86_64.rpm
XINET6_RPM=xinetd-${XINET6VerSion}.el6.x86_64.rpm
######TAR######
OPENSSL_TAR=openssl-${SSLVerSion}.tar.gz
OPENSSH_TAR=openssh-${SSHVerSion}.tar.gz
######ScrIpt######
OH_SH=openssh_cp.sh
OL_SH=openssl_cp.sh
############
LANG=en
######bak######
[ -f ${OUTok} ] && mv ${OUTok}  ${OUTok}_$(date +%s)
[ -f ${OUTerror} ] && mv ${OUTerror}   ${OUTerror}_$(date +%s)
echo "--------------------$(date +%F)--------------------" > ${OUTok}
echo "--------------------$(date +%F)--------------------" > ${OUTerror}
######gcc_check######
rpm -qa | grep ^gcc-[0-9] >> ${OUTok} 2>> ${OUTerror}
if [ $? -ne 0 ]
then
      echo -e "\033[31m ##########检测到未安装gcc，请安装后再次重试执行##########  \033[0m"
      echo  "##########检测到未安装gcc，请安装后再次重试执行##########"  >> ${OUTerror}
      exit
fi
######perl_check######
rpm -qa | grep perl-[0-9] >> ${OUTok} 2>> ${OUTerror}
if [ $? -ne 0 ]
then
      echo -e "\033[31m ##########检测到未安装perl，请安装后再次重试执行##########  \033[0m"
      echo  "##########检测到未安装perl，请安装后再次重试执行##########" >> ${OUTerror}
      exit
fi
######Dir_check######
Dircheck(){
AA="${1}";eval BB="\$$AA"
if [[ ${BB} =~ ^/+$ ]]
then
        echo "${AA} 不能为根"
        exit
fi
unset AA
unset BB
}
Dircheck OUTok
Dircheck OUTerror
Dircheck TARTFILE
######Install_softwares_check######
#Software_check(){
#[ -f ]#暂定
#}
#Software_check 5
#Software_check 6
#Software_check 7
#Software_check 8
######download_tar_script_check######
#
#
######XinetdINSTALL######
XiNet_Telnet(){
rpm -qa | grep xinetd >> ${OUTok} 2>> ${OUTerror}
if [ $? -eq 0 ]
then
      rpm -qa | grep telnet-server >> ${OUTok} 2>> ${OUTerror}
	    if [ $? -ne 0 ]
	    then

		        AA="TELNET${1}_SERVER_RPM";eval AA="${TARTFILE}/\$$AA";rpm -ivh $(echo $AA) >> ${OUTok} 2>> ${OUTerror}
		        chkconfig telnet on
            /etc/init.d/xinetd status | grep  "stopped" >> ${OUTok} 2>> ${OUTerror}
			      if [ $? -eq 0 ]
			      then
                  /etc/init.d/xinetd start
                  sleep 3600 && chkconfig telnet off && /etc/init.d/xinetd stop &
            else
                  sleep 3600 && chkconfig telnet off &
            fi

      else
            netstat -anptu  | grep ":23 " |  grep LISTEN >> ${OUTok} 2>> ${OUTerror} && /etc/init.d/xinetd status | grep running >> ${OUTok} 2>> ${OUTerror}
            if [ $? -ne 0 ]
            then
                  chkconfig telnet on
                  /etc/init.d/xinetd start
                  netstat -anptu  | grep ":23 " |  grep LISTEN >> ${OUTok} 2>> ${OUTerror} && /etc/init.d/xinetd status | grep running >> ${OUTok} 2>> ${OUTerror}
                  if [ $? -eq 0 ]
                  then
                        /etc/init.d/xinetd status | grep  "stopped" >> ${OUTok} 2>> ${OUTerror}
                        if [ $? -eq 0 ]
                        then
                              sleep 3600 && chkconfig telnet off && /etc/init.d/xinetd stop &
                        else
                              sleep 3600 && chkconfig telnet off &
                        fi
                  fi
            fi

      fi
else
      AA="XINET${1}_RPM";eval AA="${TARTFILE}/\$$AA";rpm -ivh $(echo $AA) >> ${OUTok} 2>> ${OUTerror}
	    AA="TELNET${1}_SERVER_RPM";eval AA="${TARTFILE}/\$$AA"; rpm -ivh $(echo $AA) >> ${OUTok} 2>> ${OUTerror}
	    chkconfig telnet on
	    /etc/init.d/xinetd start
	    sleep 3600 && chkconfig telnet off && /etc/init.d/xinetd stop &
fi
}
TelNet(){
rpm -qa | grep telnet-server >> ${OUTok} 2>> ${OUTerror}
if [ $? -eq 0 ]
then
      netstat -anptu  | grep ":23 " |  grep LIST >> ${OUTok} 2>> ${OUTerror} && systemctl  status  telnet.socket | grep Active | grep active >> ${OUTok} 2>> ${OUTerror}
      if [ $? -ne 0 ]
      then
            systemctl start telnet.socket
            if [ $? -eq 0 ]
            then
                  sleep 3600 && systemctl stop telnet.socket &
            fi
      fi
else
      AA="TELNET${1}_SERVER_RPM";eval AA="${TARTFILE}/\$$AA"; rpm -ivh $(echo $AA) >> ${OUTok} 2>> ${OUTerror}
fi

}
######PAMUPDATE######
Pam_UPdate(){
rpm -qa | grep "^pam-devel.*.x86_64" >> ${OUTok} 2>> ${OUTerror}
if [ $? -ne  0 ]
then
      rpm -qa | grep -E "^pam-[0-9].*x86_64" >> ${OUTok} 2>> ${OUTerror}
  	  if [ $? -eq 0 ]
  	  then
            AA="PAM${1}VerSion";eval AA="\$$AA"
  		      rpm -qa | grep -E pam-${AA}.el${1}.x86_64 >> ${OUTok} 2>> ${OUTerror}
  		      if [ $? -eq 0 ]
  		      then
  			         AA="PAM${1}_DEVEL_RPM";eval AA="${TARTFILE}/\$$AA";rpm -ivh $(echo $AA) >> ${OUTok} 2>> ${OUTerror}
  		      else
  			         AA="PAM${1}_RPM";eval AA="${TARTFILE}/\$$AA";rpm -Uvh $(echo $AA) >> ${OUTok} 2>> ${OUTerror}
  			         if [ $? -eq 0 ]
  			         then
  				             AA="PAM${1}_RPM";eval AA="${TARTFILE}/\$$AA";rpm -ivh $(echo $AA) >> ${OUTok} 2>> ${OUTerror}
  			         else
  				             echo -e "\033[31m ##########PAM_update_error##########  \033[0m"
  				             echo "error: 系统自带pam版本与脚本指定pam-devel版本不符或者其他原因无法将当前pam升级至指定pam版本" >> ${OUTerror}
                       exit
  			         fi
  		      fi
  	  else
  		      AA="PAM${1}_RPM";eval AA="${TARTFILE}/\$$AA";rpm -ivh $(echo $AA) >> ${OUTok} 2>> ${OUTerror}
  		      AA="PAM${1}_DEVEL_RPM";eval AA="${TARTFILE}/\$$AA";rpm -ivh $(echo $AA) >> ${OUTok} 2>> ${OUTerror}
  	  fi
fi
}
######WgetSoftware######
Wget_all_other(){
for i in "${OPENSSL_TAR}" "${OPENSSH_TAR}" "${ZLIB_TAR}"
do
        if [ ! -f ${TARTFILE}/${i} ]
        then
              wget -c ${Server}://${IpS}${SoftWare}/${i}  >> ${OUTok} 2>> ${OUTerror}
              if [ $? -ne 0 ]
              then
                    echo "${i} 下载失败，请重试"
                    exit
              fi
        fi
done
wget -c ${Server}://${IpS}${ScrIpt}/${OH_SH}  >> ${OUTok} 2>> ${OUTerror}
if [ ! -f ${TARTFILE}/${OH_SH} ]
then
      echo "${OH_SH} 下载失败，请重试"
      exit
fi
wget -c ${Server}://${IpS}${ScrIpt}/${OL_SH}  >> ${OUTok} 2>> ${OUTerror}
if [ ! -f ${TARTFILE}/${OH_SH} ]
then
      echo "${OH_SH} 下载失败，请重试"
      exit
fi
}

Wget_all_redhat(){
PAM=PAM${1}_RPM;eval PAM="\$$PAM"
PAM_DEVEL=PAM${1}_DEVEL_RPM;eval PAM_DEVEL="\$$PAM_DEVEL"
TELNET=TELNET${1}_SERVER_RPM;eval TELNET="\$$TELNET"
for i in   "${OPENSSL_TAR}" "${OPENSSH_TAR}" "${ZLIB_TAR}" "${XINET5_RPM}" "${XINET6_RPM}" "$PAM" "$PAM_DEVEL" "$TELNET"
do
        if [ ! -f ${TARTFILE}/${i} ]
        then
              wget -c ${Server}://${IpS}${SoftWare}/${i}  >> ${OUTok} 2>> ${OUTerror}
              if [ $? -ne 0 ]
              then
                    echo "${i} 下载失败，请重试"
                    exit
              fi
        fi
done
wget -c ${Server}://${IpS}${ScrIpt}/${OH_SH}  >> ${OUTok} 2>> ${OUTerror}
if [ ! -f ${TARTFILE}/${OH_SH} ]
then
      echo "${OH_SH} 下载失败，请重试"
      exit
fi
wget -c ${Server}://${IpS}${ScrIpt}/${OL_SH}  >> ${OUTok} 2>> ${OUTerror}
if [ ! -f ${TARTFILE}/${OH_SH} ]
then
      echo "${OH_SH} 下载失败，请重试"
      exit
fi
}

mkdir -p ${TARTFILE}
cd ${TARTFILE}
#可定义针对不同版本的wget
######redhat5/centos5######
Version_5=`cat /etc/redhat-release  | grep "release 5."  >> ${OUTok} 2>> ${OUTerror}`
if [ $? -eq 0 ]
then
      rpm -qa | grep wget >> ${OUTok} 2>> ${OUTerror} ||  rpm -ivh ${Server}://${IpS}${SoftWare}/${Wget5_RPM}
      rpm -qa | grep net-tools >> ${OUTok} 2>> ${OUTerror} || rpm -ivh ${Server}://${IpS}${SoftWare}/${Net5_RPM}
      Wget_all_redhat 5
      XiNet_Telnet 5
      Pam_UPdate 5
fi
######redhat6/centos6######
Version_6=`cat /etc/redhat-release  | grep "release 6."  >> ${OUTok} 2>> ${OUTerror}`
if [ $? -eq 0 ]
then
      rpm -qa | grep wget >> ${OUTok} 2>> ${OUTerror} || rpm -ivh ${Server}://${IpS}${SoftWare}/${Wget6_RPM}
      rpm -qa | grep net-tools >> ${OUTok} 2>> ${OUTerror} || rpm -ivh ${Server}://${IpS}${SoftWare}/${Net6_RPM}
      Wget_all_redhat 6
      XiNet_Telnet 6
      Pam_UPdate 6
fi
######redhat7/centos7######
Version_7=`cat /etc/redhat-release  | grep "release 7."  >> ${OUTok} 2>> ${OUTerror}`
if [ $? -eq 0 ]
then
      rpm -qa | grep wget >> ${OUTok} 2>> ${OUTerror} || rpm -ivh ${Server}://${IpS}${SoftWare}/${Wget7_RPM}
      rpm -qa | grep net-tools >> ${OUTok} 2>> ${OUTerror} || rpm -ivh ${Server}://${IpS}${SoftWare}/${Net7_RPM}
      Wget_all_redhat 7
      Pam_UPdate 7
      TelNet 7
fi
######redhat8/centos8######
Version_8=`cat /etc/redhat-release  | grep "release 8."  >> ${OUTok} 2>> ${OUTerror}`
if [ $? -eq 0 ]
then
      rpm -qa | grep wget >> ${OUTok} 2>> ${OUTerror} || rpm -ivh ${Server}://${IpS}${SoftWare}/${Wget8_RPM}
      rpm -qa | grep net-tools >> ${OUTok} 2>> ${OUTerror} || rpm -ivh ${Server}://${IpS}${SoftWare}/${Net8_RPM}
      Wget_all_redhat 8
      Pam_UPdate 8
      TelNet 8
fi
######other######
if [ -f /etc/redhat-release ]
then
      echo "redhat" >> ${OUTok}
else
      Wget_all_other
      if [ $? -ne 0 ]
      then
            echo "Download error"
            exit
      fi
fi

######
tar -xvf ${ZLIB_TAR} > /dev/null 2>1

cd zlib-${ZliBVerSion}

echo "################Zlibconfigre################" >> ${OUTok}
echo "################Zlibconfigre################" >> ${OUTerror}

./configure >> ${OUTok} 2>> ${OUTerror}
if [ $? -ne 0 ]
then
      echo -e "\033[31m ##########Zlibconfig_error##########  \033[0m"
      rm ${ZLIB_TAR} -f
      exit
else
      echo -e "\033[32m####################Zlib_configure_ok######################\033[0m"

fi

echo "################Zlibmake################" >> ${OUTok}
echo "################Zlibmake################" >> ${OUTerror}

make  >> ${OUTok} 2>> ${OUTerror}
if [ $? -ne 0 ]
then
      echo -e "\033[31m ##########Zlibmake_error##########  \033[0m"
      rm ${ZLIB_TAR} -f
      exit
else
      echo -e "\033[32m####################Zlib_make_ok###########################\033[0m"
fi


echo "################Zlibmakeinstall################" >> ${OUTok}
echo "################Zlibmakeinstall################" >> ${OUTerror}

make install  >> ${OUTok} 2>> ${OUTerror}
if [ $? -ne 0 ]
then
      echo -e "\033[31m ##########Zlibmakeinstall_error##########  \033[0m"
      rm ${ZLIB_TAR} -f
      exit
else
      echo -e "\033[32m####################Zlib_make_install_ok###################\033[0m"
fi

rm ${ZLIB_TAR} -f
######
openssl version >/tmp/sslversiontemp
grep "${SSLVerSion}" /tmp/sslversiontemp  >> ${OUTok} 2>> ${OUTerror}
if [ $? -ne 0 ]
then
      sh ${TARTFILE}/${OL_SH} ${SSLVerSion} ${SSLPREHOME} ${OUTok} ${OUTerror} ${TARTFILE} &
      sleep 10
      OPENSSL_PID=$(cat /tmp/openssl_updateshell_pid)
      while :
      do
            ps aux | grep -v grep | grep ${OPENSSL_PID}  >> ${OUTok} 2>> ${OUTerror}
            if [ $? -eq 0 ]
            then
                  echo -e '请等待 -'"\b\b\b\b\b\b\b\b\c"
                  sleep 0.15
                  echo -e '请等待 \\'"\b\b\b\b\b\b\b\b\c"
                  sleep 0.15
                  echo -e '请等待 |'"\b\b\b\b\b\b\b\b\c"
                  sleep 0.15
                  echo -e '请等待 /'"\b\b\b\b\b\b\b\b\c"
            else
                  break
            fi
      done

      openssl version >/tmp/sslversiontemp
      grep "${SSLVerSion}" /tmp/sslversiontemp  >> ${OUTok} 2>> ${OUTerror}
      if [ $? -ne 0 ]
      then
            echo -e "\033[31m ##########OPENSSL_error##########  \033[0m"
            rm -f ${TARTFILE}/openssl-${SSLVerSion}
            exit
      else
            echo -e "\033[32mopenssl_end#########OPENSSL_INSTALL_OK##########openssl_end\033[0m"
            #echo -e "\033[32m####################OPENSSL_INSTALL_OK#####################\033[0m"
      fi

else
      echo -e "\033[32m####################OPENSSL_IS_ALREADY_THE_SPECIFIED_VERSION_####################\033[0m"
fi
######
df | grep /var$ >> ${OUTok} 2>> ${OUTerror}
if [ $? -eq  0 ]
then
      AA=`df | grep /var$ | awk '{ print $3 }'`
      if [ ${AA} -lt 10240 ]
      then
            echo -e "\033[31m ##########内存小于10M##########  \033[0m"
            echo "##########内存小于10M##########" >> ${OUTerror}
      fi
else
      AA=`df | grep /$ |  awk '{ print $3 }'`
      if [ ${AA} -lt 10240 ]
      then
            echo -e "\033[31m ##########内存小于10M##########  \033[0m"
            echo "##########内存小于10M##########" >> ${OUTerror}
      fi
fi
######
ssh -V 2>/tmp/sshversiontemp
grep "${SSHVerSion}" /tmp/sshversiontemp  >> ${OUTok} 2>> ${OUTerror}
if [ $? -ne 0 ]
then
      sh ${TARTFILE}/${OH_SH} ${SSLVerSion} ${SSHVerSion} ${SSLPREHOME} ${SSHPREHOME} ${OUTok} ${OUTerror} ${TARTFILE} &
      sleep 10
      OPENSSH_PID=$(cat /tmp/openssh_updateshell_pid)
      while :
      do
            ps aux | grep -v grep | grep ${OPENSSH_PID}  >> ${OUTok} 2>> ${OUTerror}
            if [ $? -eq 0 ]
            then

                  echo -e '请等待 -'"\b\b\b\b\b\b\b\b\c"
                  sleep 0.15
                  echo -e '请等待 \\'"\b\b\b\b\b\b\b\b\c"
                  sleep 0.15
                  echo -e '请等待 |'"\b\b\b\b\b\b\b\b\c"
                  sleep 0.15
                  echo -e '请等待 /'"\b\b\b\b\b\b\b\b\c"

            else
                  break
            fi
      done
      ssh -V 2>/tmp/sshversiontemp
      grep "${SSHVerSion}" /tmp/sshversiontemp  >> ${OUTok} 2>> ${OUTerror}
      if [ $? -ne 0 ]
      then
            echo -e "\033[31m ##########OPENSSH_ERROR##########  \033[0m"
            rm -f ${TARTFILE}/openssh-${SSLVerSion}
            exit
      else
            echo -e "\033[32mopenssh_end#########OPENSSH_INSTALL_OK##########openssh_end\033[0m"
            #echo -e "\033[32m####################OPENSSH_INSTALL_OK#####################\033[0m"
      fi
else
      echo -e "\033[32m####################OPENSSH_IS_ALREADY_THE_SPECIFIED_VERSION_####################\033[0m"
fi
##########################################
rm -f ${TARTFILE}/*.tar.gz
rm -f ${TARTFILE}/*.rpm
rm -rf ${TARTFILE}/openssl-${SSLVerSion}
rm -rf ${TARTFILE}/openssh-${SSHVerSion}
rm -rf ${TARTFILE}/zlib-${ZliBVerSion}
rm -f ${TARTFILE}/openssh_cp.sh
rm -f ${TARTFILE}/openssl_cp.sh
sleep 600 && rm -f /tmp/$0 &
